package cdu.notFound.Dao.impl;
import cdu.notFound.Dao.MessDao;
import cdu.notFound.model.Mess;
import cdu.notFound.model.User;
import cdu.notFound.utils.DatabaseConnection;
import cdu.notFound.utils.IntHolder;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class MessDaoImpl implements MessDao {
    private Connection conn = null;
    private PreparedStatement pstmt = null;
    public MessDaoImpl() {
        try {
            this.conn = new DatabaseConnection().getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public boolean doCreate(Mess mess) {
        boolean flag = false;
        String sql = "INSERT INTO `message`(mess_from_id,mess_to_id,mess_text,send_time) VALUES (?,?,?,?)";
        try {
            this.pstmt = this.conn.prepareStatement(sql);
            pstmt.setInt(1, mess.getMessFromId());
            pstmt.setInt(2, mess.getMessToId());
            pstmt.setString(3, mess.getMessText());
            pstmt.setTimestamp(4,new Timestamp(mess.getSendDate().getTime()));
            if (this.pstmt.executeUpdate() > 0) {
                flag = true;
            }
            this.pstmt.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return flag;
    }
    
    public List<Mess> findAllMessByUser(IntHolder num, User user, int limitMin, int perPage){
        List<Mess> all = new ArrayList<Mess>();
        String sql = "SELECT SQL_CALC_FOUND_ROWS mess_id,mess_from_id,mess_to_id,send_time,mess_text from `message`  where mess_to_id=? order by send_time desc limit ?,?";
        try {
            this.pstmt = this.conn.prepareStatement(sql);
            this.pstmt.setInt(1,user.getId());
            this.pstmt.setInt(2,limitMin);
            this.pstmt.setInt(3,perPage);
            ResultSet rs = this.pstmt.executeQuery();
            while (rs.next()) {
                Mess mess = new Mess();
                mess.setMessId(rs.getInt(1));
                mess.setMessFromId(rs.getInt(2));
                mess.setMessToId(rs.getInt(3));
                Timestamp timeStamp=rs.getTimestamp(4);
                Date date=new Date(timeStamp.getTime());
                mess.setSendDate(date);
                mess.setMessText(rs.getString(5));
                all.add(mess);
            }
            rs = this.pstmt.executeQuery("SELECT FOUND_ROWS()");
            if(rs.next()){
                num.value=rs.getInt(1);
            }
            rs.close();this.pstmt.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return all;
    }
    
    public boolean removeOneMess(int messId,int userId){
        Boolean flag=false;
        String sql="Delete from `message` where mess_id=? and mess_to_id=?";
        try {
            pstmt=conn.prepareStatement(sql);
            pstmt.setInt(1, messId);
            pstmt.setInt(2,userId);
            if (this.pstmt.executeUpdate() > 0) {
                flag = true;
            }
            this.pstmt.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return flag;
    }
    
	public void close(){
		if(this.conn != null){
			try{
				this.conn.close();
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	}
}
